package com.smz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TReservoir;
import com.smz.entity.TShelves;
import com.smz.service.TReservoirService;
import com.smz.service.TShelvesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class TShelvesController {
    @Autowired
    private TShelvesService tShelvesService;
    @Autowired
    private TReservoirService tReservoirService;

    @ApiOperation("通过货区名 来获得货架")
    @GetMapping("/tshelvesById/{id}")
    public ResponseEntity<List<TShelves>> findByDy(@PathVariable("id") String id) {
        List<TShelves> list = tShelvesService.list(new QueryWrapper<TShelves>().lambda().eq(TShelves::getDameId, id));
        return ResponseEntity.ok(list);
    }

    @ApiOperation("查所有货架")
    @GetMapping("/tshelvesdy")
    public ResponseEntity<Page<TShelves>> findDy(@RequestParam("current") Integer current, @RequestParam("row") Integer row,
                                                 @RequestParam("shelvesNum") String shelvesNum, @RequestParam("time") String time) {

        Page<TShelves> page = new Page(current, row);
        Page<TShelves> page1;
        if (shelvesNum.equals("null")||shelvesNum.length()==0) {
            if (time.equals("null")) {
                page1 = tShelvesService.page(page);
                for (TShelves sh:page1.getRecords()) {
                    TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, sh.getDameId()));
                    if (one.getStatus()==1){

                    }else{
                        sh.setStatus("0");
                    }
                }
                page1.setTotal(tShelvesService.count());
            } else {
                page1 = tShelvesService.page(page, new QueryWrapper<TShelves>().lambda().likeRight(TShelves::getCreatTime, time));
                for (TShelves sh:page1.getRecords()) {
                    TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, sh.getDameId()));
                    if (one.getStatus()==1){

                    }else{
                        sh.setStatus("0");
                    }
                }
                page1.setTotal(tShelvesService.count());
            }
        } else {
            if (time.equals("null")) {
                page1 = tShelvesService.page(page, new QueryWrapper<TShelves>().lambda().eq(TShelves::getShelvesNo, shelvesNum));
                for (TShelves sh:page1.getRecords()) {
                    TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, sh.getDameId()));
                    if (one.getStatus()==1){
                    }else{
                        sh.setStatus("0");
                    }
                }
                page1.setTotal(tShelvesService.count());
            } else {
                page1 = tShelvesService.page(page, new QueryWrapper<TShelves>().lambda().likeRight(TShelves::getCreatTime, time).eq(TShelves::getShelvesNo, shelvesNum));
                for (TShelves sh:page1.getRecords()) {
                    TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, sh.getDameId()));
                    if (one.getStatus()==1){
                    }else{
                        sh.setStatus("0");
                    }
                }
                page1.setTotal(tShelvesService.count());
            }
        }
        return ResponseEntity.ok(page1);
    }

    @ApiOperation("删除货架")
    @DeleteMapping("/tshelvesDy/{id}")
    public ResponseEntity<String> findDy(@PathVariable("id") Integer id) {
        tShelvesService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("查一个货架")
    @GetMapping("/tshelvesDy/{id}/{status}")
    public ResponseEntity<String> findOneDy(@PathVariable("id") Integer id, @PathVariable("status") String status) {
        TShelves byId = tShelvesService.getById(id);
        byId.setStatus(status);
        tShelvesService.saveOrUpdate(byId);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("通过货区的wareId查货区")
    @GetMapping("/tReservoirDy/{dameId}")
    public ResponseEntity<TReservoir> findOneDy(@PathVariable("dameId") Integer dameId) {
        TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, dameId));
        return ResponseEntity.ok(one);
    }

    @ApiOperation("更新货架")
    @PutMapping("/tshelvesDy")
    public ResponseEntity<String> updateOneDy(@RequestBody TShelves tShelves) {
        tShelvesService.saveOrUpdate(tShelves);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("添加货架")
    @PostMapping("/tshelvesDy")
    public ResponseEntity<String> addOneDy(@RequestBody TShelves tShelves) {
        tShelvesService.saveOrUpdate(tShelves);
        return ResponseEntity.ok("ok");
    }

    /**
     * @Authoe: smz
     * @Description: 下拉框根据库区id获取当前货区里的所有货架
     * @Date: 10:44 2021-11-27
     */
    @GetMapping("findShelvesByReservoirId/{id}")
    public ResponseEntity<List<TShelves>> findByReservoirId(@PathVariable("id")Integer id){
        List<TShelves> list = tShelvesService.list(new QueryWrapper<TShelves>().lambda().eq(TShelves::getDameId ,id));
        return ResponseEntity.ok(list);
    }
}
